﻿using Edu.Abstracts;
using Edu.Entitys;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace Edu.Services
{

    public class RatingInDto
    {
        public Guid TargetId { get; set; }
        public int Year { get; set; }
        public int Quarter { get; set; }
        public int Score { get; set; }
    }


    public class RatingAppService : WeChatAppServiceBase
    {
        public IRepository<AppRating, Guid> Repository { get; }
        private IRepository<AppStudent, Guid> appStudents;
        private IRepository<AppTeacher, Guid> appTeachers;
        public RatingAppService(IRepository<AppRating, Guid> ratings,
            IRepository<AppStudent, Guid> appStudents,
            IRepository<AppTeacher, Guid> appTeachers
            )
        {
            this.appStudents = appStudents;
            this.appTeachers = appTeachers;
            this.Repository = ratings;
        }

        public async Task<object> ToTeacher(RatingInDto inDto)
        {
            var appRating = await GetOrAddRating(this.CurrentUser.Id.Value, inDto.TargetId, inDto.Year, inDto.Quarter);

            var oldScore = appRating.TeacherScore ?? 0;

            appRating.TeacherScore = inDto.Score;
            appRating.TeacherTime = DateTimeOffset.Now;

            await this.Repository.UpdateAsync(appRating, true);

            //更新总评分用于排序

            var teacherInfo = appTeachers.Single(a => a.UserId == inDto.TargetId);
            teacherInfo.Rating += inDto.Score - oldScore;
            if (teacherInfo.Rating <= 0)
            {
                teacherInfo.Rating = 0;
            }
            await appTeachers.UpdateAsync(teacherInfo, true);

            return new { };
        }

        public async Task<object> ToStudent(RatingInDto inDto)
        {
            var appRating = await GetOrAddRating(inDto.TargetId, this.CurrentUser.Id.Value, inDto.Year, inDto.Quarter);
            var oldScore = appRating.StudentScore ?? 0;
            appRating.StudentScore = inDto.Score;
            appRating.StudentTime = DateTimeOffset.Now;
            await this.Repository.UpdateAsync(appRating, true);


            var studentInfo = appStudents.Single(a => a.UserId == inDto.TargetId);
            studentInfo.Rating += inDto.Score - oldScore;
            if (studentInfo.Rating <= 0)
            {
                studentInfo.Rating = 0;
            }
            await appStudents.UpdateAsync(studentInfo, true);
            return new { };
        }

        public async Task<AppRating> GetOrAddRating(Guid studentId, Guid teacherId, int year, int quarter)
        {
            var result = this.Repository.Where(s => s.StudentId == studentId && s.TeacherId == teacherId && s.Year == year && s.Quarter == quarter)
                .FirstOrDefault();

            if (result == null)
            {
                result = await this.Repository.InsertAsync(new AppRating()
                {
                    Id = GuidGenerator.Create(),
                    Year = year,
                    Quarter = quarter,
                    TeacherId = teacherId,
                    StudentId = studentId,
                }, true);
            }
            return result;
        }
    }
}
